package com.millstein.realtime.util;

import com.alibaba.fastjson.JSONObject;
import com.millstein.realtime.common.Constants;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description
 * @Author tsing
 * @Date 2024-09-26 15:43
 */
@Slf4j
public class JdbcUtil {

    /**
     * 获取phoenix的连接对象
     * @return
     */
    public static Connection getPhoenixConnection() {
        return getJdbcConnection(Constants.PHOENIX_DRIVER, Constants.PHOENIX_URL, null, null);
    }

    /**
     * 获取jdbc的连接对象
     * @param driver 驱动类
     * @param url jdbc地址
     * @param username 用户名
     * @param password 密码
     * @return
     */
    public static Connection getJdbcConnection(String driver, String url, String username, String password) {
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            log.error("驱动类配置有误，当前的驱动类为：{}", driver, e);
            throw new RuntimeException(e);
        }

        try {
            return DriverManager.getConnection(url, username, password);
        } catch (SQLException e) {
            log.error("获取连接发生异常，请检查配置，当前的配置为url = {}, username = {}, password = {}", url, username, password, e);
            throw new RuntimeException(e);
        }
    }

    /**
     * 关闭连接
     * @param connection 待关闭的连接对象
     */
    public static void closeConnection(Connection connection) {
        try {
            if (connection != null && !connection.isClosed()) {
                connection.close();
            }
        } catch (SQLException e) {
            log.error("关闭连接发生异常", e);
            throw new RuntimeException(e);
        }
    }

    /**
     * 从数据库中查询数据
     * @param connection 数据库连接
     * @param sql 待查询的sql语句
     * @param args sql语句的参数
     * @param clazz 返回的类型
     * @return
     */
    public static <T> List<T> queryList(Connection connection, String sql, Object[] args, Class<T> clazz) {
        List<T> list = new ArrayList<>();
        ResultSet resultSet = null;
        PreparedStatement pstm = null;
        try {
            pstm = connection.prepareStatement(sql);
            if (args != null) {
                for (int i = 0; i < args.length; i++) {
                    pstm.setObject(i + 1, args[i]);
                }
            }

            resultSet = pstm.executeQuery();
            ResultSetMetaData metaData = resultSet.getMetaData();
            int columnCount = metaData.getColumnCount();

            while (resultSet.next()) {
                T t = clazz.newInstance();
                for (int i = 0; i < columnCount; i++) {
                    String column = metaData.getColumnLabel(i + 1);
                    Object value = resultSet.getObject(i + 1);
                    BeanUtils.setProperty(t, column, value);
                }
                list.add(t);
            }

            resultSet.close();
            pstm.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return list;
    }

}
